#include "stdafx.h"
#include "MainSystem.h"

SmartPtr <MainSystem> g_pSystem_Main;

MainSystem :: MainSystem() : mbExit(false), mbInitialized(false)
{
}

MainSystem :: ~MainSystem()
{
}

bool MainSystem :: Initialize()
{
	m_pHGESystem = SmartPtr<HGE_Impl>((HGE_Impl *)hgeCreate(HGE_VERSION));
	m_pHGESystem->System_SetState(HGE_TITLE,		APP_TITLE);
	m_pHGESystem->System_SetState(HGE_ICON,			(char *)MAKEINTRESOURCE(IDI_MOMOGENKYOUYUMEMONOGATARI));
	m_pHGESystem->System_SetState(HGE_SCREENWIDTH,	APP_WIDTH);
	m_pHGESystem->System_SetState(HGE_SCREENHEIGHT, APP_HEIGHT);
	m_pHGESystem->System_SetState(HGE_FPS,			60);

	m_pHGESystem->System_SetState(HGE_WINDOWED,		true);
	m_pHGESystem->System_SetState(HGE_USESOUND,		false);
	m_pHGESystem->System_SetState(HGE_SHOWSPLASH,	false);

	m_pHGESystem->System_SetState(HGE_ZBUFFER,			true);
	m_pHGESystem->System_SetState(HGE_TEXTUREFILTER,	true);
	m_pHGESystem->System_SetState(HGE_DONTSUSPEND,		true);
	m_pHGESystem->System_SetState(HGE_HIDEMOUSE,		false);

	m_pHGESystem->System_SetState(HGE_FRAMEFUNC,	MainSystem::UpdateFrame);
	m_pHGESystem->System_SetState(HGE_RENDERFUNC,	MainSystem::RenderFrame);
	m_pHGESystem->System_SetState(HGE_EXITFUNC,		ExitFrame);

	if(!m_pHGESystem->System_Initiate())
	{
		WriteCommandLine("Hge system failed to Initialize");
		return false;
	}
	m_pHGESystem->Resource_AttachPack("Data/Resource.zip");
	m_pResources = SmartPtr<hgeResourceManager>(new hgeResourceManager("Data/ResourceDefinition.ini"));

	g_pSystem_Console		= SmartPtr<System_Console>(new System_Console(APP_WIDTH, APP_HEIGHT));
	g_pSystem_HistoryGraph	= SmartPtr<System_HistoryGraph>(new System_HistoryGraph);
	g_pSystem_Audio			= SmartPtr<System_Audio_Hook>(new System_Audio_Hook(m_pHGESystem.get()));

	m_pFontManager = SmartPtr<hgeTTF::FontManager>(new hgeTTF::FontManager());
	if(m_pFontManager->Initialize())
	{
		if(!m_pFontManager->loadFont("Data/Effects/Font.ttf"))
			WriteCommandLine("Error Loading Font %s", "Data/Effects/Font.ttf");
	}
	else
		WriteCommandLine("Error with Font Manager");

	m_pSystem_PluginManager = SmartPtr<System_PluginManager>(new System_PluginManager);

	g_pInterface_SplashScreen = SmartPtr<Interface_SplashScreen>(new Interface_SplashScreen);
	g_pInterface_SplashScreen->Initailize();
	return mbInitialized = true;
}

bool MainSystem :: ShutDown()
{
	m_pFontManager->unloadFonts();
	m_pFontManager.reset(NULL);
	m_pFontManager.release();

	g_pSystem_Audio.reset(NULL);
	g_pSystem_Audio.release();

	g_pSystem_HistoryGraph.reset(NULL);
	g_pSystem_HistoryGraph.release();

	m_pSystem_PluginManager.reset(NULL);
	m_pSystem_PluginManager.release();
	/* Delete last */
	g_pSystem_Console.reset(NULL);
	g_pSystem_Console.release();

	m_pResources.reset(NULL);
	m_pResources.release();

	m_pHGESystem->Resource_RemoveAllPacks();
	m_pHGESystem->System_Shutdown();
	m_pHGESystem->Release();
	m_pHGESystem.release();
	return true;
}

bool MainSystem :: ExitFrame()
{
	return g_pSystem_Main->mbExit = true;
}

bool MainSystem :: UpdateFrame()
{
	/* Update All Components here */
#ifdef _DEBUG
	if(g_pSystem_Main->HGESYSTEM()->Input_KeyDown(VK_ESCAPE))
		return g_pSystem_Main->mbExit = true;
#endif

	const float Interval = g_pSystem_Main->HGESYSTEM()->Timer_GetDelta();
	if(KeyPressed(VK_OEM_3)) g_pSystem_Console->OnOffConsole();

	g_pSystem_Console->Update(Interval);
	
	g_pInterface_SplashScreen->Update(Interval);

	return g_pSystem_Main->mbExit;	// true to exit
}

bool MainSystem :: RenderFrame()
{
	if(!g_pSystem_Main->mbInitialized) return true;
	g_pSystem_Main->m_pHGESystem->Gfx_BeginScene();
	g_pSystem_Main->m_pHGESystem->Gfx_Clear(ARGB(255, 45, 45, 120));
	/* Render All Components here */

	g_pInterface_SplashScreen->Render();



	g_pSystem_Console->Render();
	if(g_pSystem_HistoryGraph->IsActive())
		g_pSystem_HistoryGraph->Render();
	g_pSystem_Main->m_pHGESystem->Gfx_EndScene();
	return false;
}